<- "hello world" # assigns the value 'hello world' to variable [greeting]
greeting print(greeting) # print the value of [greeting] to the console
[1] "hello world"
The pre-class reading for this section covered a number of topics, which we’ll revise today:
Variables and Assignment
Data Types in R
Vectors and Vector Operations
We can create variables, operate on variables, and update variables.
Create:
<- "hello world" # assigns the value 'hello world' to variable [greeting]
greeting print(greeting) # print the value of [greeting] to the console
[1] "hello world"
Operate:
<- 10
x <- 5
y <- x + y # adds the values of each variable
sum print(sum)
[1] 15
Update:
<- 0 # set [turn] to zero
turn <- turn + 1 # increment [turn] by 1
turn print(turn) # print updated value of 'turn'
[1] 1
Objective: Learn how to assign a value to a variable.
print()
function.<- 10
x <- 25
y
print(x) # Should output: [1] 10
print(y) # Should output: [1] 25
Objective: Understand how to use variables in arithmetic operations.
<- 5
a <- 3
b
<- a + b
sum_result <- a * b
product_result
print(sum_result) # Should output: [1] 8
print(product_result) # Should output: [1] 15
Objective: Learn how to update the value of a variable.
Assign the value 50 to a variable named [total].
Increase the value of [total] by 20 using the <-
operator.
Decrease the value of [total] by 10.
Print the updated value of [total].
<- 50
total <- total + 20
total <- total - 10
total
print(total) # Should output: [1] 60
Objective: Practice using variables in a more complex expression.
[area] = [length] * [width]
.[perimeter] = 2 * ([length] + [width])
.<- 4
length <- 6
width
<- length * width
area <- 2 * (length + width)
perimeter
print(area) # Should output: [1] 24
print(perimeter) # Should output: [1] 20
Objective: Understand how to manipulate and swap variable values.
<- 7
m <- 14
n
# Swapping values
<- m
temp <- n
m <- temp
n
print(m) # Should output: [1] 14
print(n) # Should output: [1] 7
Objective: Learn to identify and understand different data types.
class()
function.<- 42
num class(num) # Should output: [1] "numeric"
<- "John Doe"
name class(name) # Should output: [1] "character"
<- TRUE
is_raining class(is_raining) # Should output: [1] "logical"
Objective: Practice converting data between different types.
<- 5
a <- as.character(a)
a_char <- as.numeric(a_char)
a_num
print(a_char) # Should output: [1] "5"
print(class(a_char)) # Should output: [1] "character"
print(a_num) # Should output: [1] 5
print(class(a_num)) # Should output: [1] "numeric"
Objective: Understand how logical values work in R.
<- 10
x <- 15
y
<- x > y
is_greater <- x == y
is_equal
print(is_greater) # Should output: [1] FALSE
print(is_equal) # Should output: [1] FALSE
Objective: Get familiar with the factor data type, often used for categorical data.
class()
.levels()
function to display the levels of [colour_factor].<- c("red", "blue", "green", "blue", "red")
colours <- factor(colours)
colour_factor
print(colour_factor)
# Should output:
# [1] red blue green blue red
# Levels: blue green red
print(class(colour_factor)) # Should output: [1] "factor"
print(levels(colour_factor)) # Should output: [1] "blue" "green" "red"
Objective: Explore how combining different data types in a vector results in coercion to a single data type.
class()
.<- c(5, "hello", TRUE)
mixed
print(mixed)
# Should output: [1] "5" "hello" "TRUE"
print(class(mixed)) # Should output: [1] "character"
# Try a numeric operation
# mixed_numeric <- mixed + 2 ### Note this line should be run, but has to be commented as it returns an error
# This will result in an error because the vector is of character type.
Objective: Understand how to check the length of vectors and why it’s important.
length()
function to check the length of both vectors.<- c(10, 20, 30, 40, 50)
numbers <- c("a", "b", "c")
letters
print(length(numbers)) # Should output: [1] 5
print(length(letters)) # Should output: [1] 3
These tasks will help you implement some of the techniques discussed earlier in the tutorial.
Try to complete these tasks without looking at the solutions!
Write an R script to create a vector named [player_id] that contains the integers from 1 to 10.
<- 1:10
player_id print(player_id)
Create two vectors [var_a] and [var_b]. [var_a] should contain numbers from 1 to 5, and [var_b] contain numbers from 6 to 10. Then add, subtract, multiply, and divide these vectors.
<- 1:5
a <- 6:10
b print(a + b)
print(a - b)
print(a * b)
print(a / b)
Create a vector [total_goals] with the values 15, 23, 17, 21, 19. Use a logical operator to determine which values stored in the vector [total_goals] are greater than 18.
<- c(15, 23, 17, 21, 19)
total_goals print(total_goals > 18)
With the vector [total_goals] from the previous exercise, use indexing to print the 2nd and 4th values in the vector [total_goals].
print(total_goals[c(2,4)])
Working with the vector [total_goals], find the minimum, maximum, sum, and mean of the [total_goals].
Print these values to the console window.
print(min(total_goals))
print(max(total_goals))
print(sum(total_goals))
print(mean(total_goals))
Create a named vector [players_signed] that contains the number of forwards, midfielders, defense, and goalkeepers. Set the values to 10, 8, 8, and 3, respectively.
<- c(forwards = 10, midfielders = 8, defense = 8, goalkeepers = 3)
players_signed print(players_signed)
Using the vector [players_signed] from the previous exercise, add 5 to the number of forwards, subtract 2 from the number of midfielders, and print the updated vector.
"forwards"] <- players_signed["forwards"] + 5
players_signed["midfielders"] <- players_signed["midfielders"] - 2
players_signed[
print(players_signed)